package com.example.robotwatcher.backend;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DataCollector {
    String TargetUrl;
    String TAG="DataCollector";
    JSONObject DeviceInfo;
    JSONObject SystemInfo;
    JSONObject StateInfo;
    Handler mhandler;

    public DataCollector(Handler handler)
    {
        mhandler = handler;
    }


    public void setTargetUrl(String ipAddress)
    {
        TargetUrl = "http://"+ipAddress+":8000";
    }

    /// 获取设备信息，静态
    public void getDeviceInfo()
    {
        String url = TargetUrl+"/DeviceInfo";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = Message.obtain();
                if(message.obj==null)
                    Log.d(TAG,"Connect Fail");
                else
                    Log.d(TAG, "onFailure: "+message.obj.toString());
            }

            @Override
            public void onResponse(Call call, Response response) {
//                Log.d(TAG, "onResponse: " + response.body().string());
                try {
                    String content = response.body().string();
                    Log.d(TAG,content);
                    DeviceInfo = new JSONObject(content);
                    //TODO  发送消息到UI
                    Message message = Message.obtain();
                    message.what = 0;
                    message.obj = DeviceInfo;
                    mhandler.sendMessage(message);
                } catch (JSONException | IOException e) {
                    e.printStackTrace();
                }
            }
        });


    }


    /// 获取状态信息动态
    public void getStateInfo()  {
        String url = TargetUrl+"/StateInfo";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        while (true)
        {
            try
            {
                Thread.sleep(500);
            }catch (InterruptedException e){}

            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Message message = Message.obtain();
                    if(message.obj==null)
                        Log.d(TAG,"Connect Fail");
                    else
                        Log.d(TAG, "onFailure: "+message.obj.toString());
                }

                @Override
                public void onResponse(Call call, Response response)  {
//                Log.d(TAG, "onResponse: " + response.body().string());

                    try {
                        String content = response.body().string();
                        Log.d(TAG, "onResponse: "+content);
                        StateInfo = new JSONObject(content);
                        Log.d(TAG,StateInfo.toString());
                        //TODO  发送消息到UI
                        Message message = Message.obtain();
                        message.what = 0;
                        message.obj = StateInfo;
                        mhandler.sendMessage(message);

                    } catch (JSONException | IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }


    /// 获取系统信息动态
    public void getSystemInfo() {
        String url = TargetUrl+"/SystemInfo";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        while (true)
        {

            try
            {
                Thread.sleep(500);
            }catch (InterruptedException e){}
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Message message = Message.obtain();
                    if(message.obj==null)
                        Log.d(TAG,"Connect Fail");
                    else
                        Log.d(TAG, "onFailure: "+message.obj.toString());
                }

                @Override
                public void onResponse(Call call, Response response) {
//                Log.d(TAG, "onResponse: " + response.body().string());

                    try {
                        String content = response.body().string();
                        SystemInfo = new JSONObject(content);
                        Log.d(TAG,content);
                        //TODO  发送消息到UI
                        Message message = Message.obtain();
                        message.what = 0;
                        message.obj = SystemInfo;
                        mhandler.sendMessage(message);
                    } catch (JSONException | IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }
}
